select a.filial_origem as origem,a.filial as destino,a.numero_nf_transferencia as nota_fiscal,
CONVERT(varchar(30), a.emissao, 103) as emissao,b.produto as produto,b.preco1 as preco,b.qtde_entrada as qtd,
b.valor as valor,b.cor_produto as cor,C.DESC_COR_PRODUTO as nome_cor_produto,
cast(
CASE WHEN B.EN1 > 0 THEN cast(' ' +ltrim(cast(B.EN1 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_1)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN2 > 0 THEN cast(' ' +ltrim(cast(B.EN2 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_2)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN3 > 0 THEN cast(' ' +ltrim(cast(B.EN3 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_3)) + ')' as varchar(10))
ELSE '' END  +
CASE WHEN B.EN4 > 0 THEN cast(' ' +ltrim(cast(B.EN4 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_4)) + ')' as varchar(10))
ELSE '' END + 
CASE WHEN B.EN5 > 0 THEN cast(' ' +ltrim(cast(B.EN5 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_5)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN6 > 0 THEN cast(' ' +ltrim(cast(B.EN6 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_6)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN7 > 0 THEN cast(' ' +ltrim(cast(B.EN7 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_7)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN8 > 0 THEN cast(' ' +ltrim(cast(B.EN8 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_8)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN9 > 0 THEN cast(' ' +ltrim(cast(B.EN9 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_9)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN10 > 0 THEN cast(' ' +ltrim(cast(B.EN10 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_10)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN11 > 0 THEN cast(' ' +ltrim(cast(B.EN11 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_11)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN12 > 0 THEN cast(' ' +ltrim(cast(B.EN12 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_12)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN13 > 0 THEN cast(' ' +ltrim(cast(B.EN13 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_13)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN14 > 0 THEN cast(' ' +ltrim(cast(B.EN14 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_14)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN15 > 0 THEN cast(' ' +ltrim(cast(B.EN15 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_15)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN16 > 0 THEN cast(' ' +ltrim(cast(B.EN16 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_16)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN17 > 0 THEN cast(' ' +ltrim(cast(B.EN17 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_17)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN18 > 0 THEN cast(' ' +ltrim(cast(B.EN18 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_18)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN19 > 0 THEN cast(' ' +ltrim(cast(B.EN19 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_19)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN20 > 0 THEN cast(' ' +ltrim(cast(B.EN20 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_20)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN21 > 0 THEN cast(' ' +ltrim(cast(B.EN21 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_21)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN22 > 0 THEN cast(' ' +ltrim(cast(B.EN22 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_22)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN23 > 0 THEN cast(' ' +ltrim(cast(B.EN23 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_23)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN24 > 0 THEN cast(' ' +ltrim(cast(B.EN24 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_24)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN25 > 0 THEN cast(' ' +ltrim(cast(B.EN25 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_25)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN26 > 0 THEN cast(' ' +ltrim(cast(B.EN26 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_26)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN27 > 0 THEN cast(' ' +ltrim(cast(B.EN27 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_27)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN28 > 0 THEN cast(' ' +ltrim(cast(B.EN28 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_28)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN29 > 0 THEN cast(' ' +ltrim(cast(B.EN29 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_29)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN30 > 0 THEN cast(' ' +ltrim(cast(B.EN30 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_30)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN31 > 0 THEN cast(' ' +ltrim(cast(B.EN31 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_31)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN32 > 0 THEN cast(' ' +ltrim(cast(B.EN32 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_32)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN33 > 0 THEN cast(' ' +ltrim(cast(B.EN33 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_33)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN34 > 0 THEN cast(' ' +ltrim(cast(B.EN34 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_34)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN35 > 0 THEN cast(' ' +ltrim(cast(B.EN35 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_35)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN36 > 0 THEN cast(' ' +ltrim(cast(B.EN36 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_36)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN37 > 0 THEN cast(' ' +ltrim(cast(B.EN37 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_37)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN38 > 0 THEN cast(' ' +ltrim(cast(B.EN38 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_38)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN39 > 0 THEN cast(' ' +ltrim(cast(B.EN39 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_39)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN40 > 0 THEN cast(' ' +ltrim(cast(B.EN40 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_40)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN41 > 0 THEN cast(' ' +ltrim(cast(B.EN41 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_41)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN42 > 0 THEN cast(' ' +ltrim(cast(B.EN42 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_42)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN43 > 0 THEN cast(' ' +ltrim(cast(B.EN43 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_43)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN44 > 0 THEN cast(' ' +ltrim(cast(B.EN44 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_44)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN45 > 0 THEN cast(' ' +ltrim(cast(B.EN45 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_45)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN46 > 0 THEN cast(' ' +ltrim(cast(B.EN46 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_46)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN47 > 0 THEN cast(' ' +ltrim(cast(B.EN47 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_47)) + ')' as varchar(10))
ELSE '' END +
CASE WHEN B.EN48 > 0 THEN cast(' ' +ltrim(cast(B.EN48 as varchar(4))) + ' (' + rtrim(ltrim(E.TAMANHO_48)) + ')' as varchar(10))
ELSE '' END 
as varchar(400)) as qtd_tam
from loja_entradas a
join LOJA_ENTRADAS_PRODUTO b on (a.romaneio_produto = b.romaneio_produto and a.filial = b.filial)
join PRODUTO_CORES C on (B.produto = C.produto and B.cor_produto = C.cor_produto)
join PRODUTOS D on (C.produto = D.produto)
JOIN PRODUTOS_TAMANHOS E ON (D.GRADE = E.GRADE)
where substring(a.filial,1,3) = '525'
and a.TIPO_ENTRADA_SAIDA = '01'
and (
(substring(a.filial_origem,1,3) = '128' and cast(a.numero_nf_transferencia as int) in (711,713)) or 
(substring(a.filial_origem,1,3) = '124' and cast(a.numero_nf_transferencia as int) in (743,742,745,741,752,753,746,748,750,751,744,756,759,760,757,761,758)) or 
(substring(a.filial_origem,1,3) = '095' and cast(a.numero_nf_transferencia as int) in (1069,1070,1068)) or 
(substring(a.filial_origem,1,3) = '999' and rtrim(ltrim(a.numero_nf_transferencia)) = '280218'))
order by a.filial_origem,a.numero_nf_transferencia
